<template>
  <div class="mod-config">
    <!-- 查询条件 -->
    <div class="search-form search_form" v-loading='loadingPage'>
      <el-form :model="searchForm" size="mini" label-position="right" class="demo-form-inline" label-width="110px">
        <el-row :gutter="10">

          <el-col :span="5">
            <el-form-item label="部门名称：">
              <el-select v-model="searchForm.deptId" placeholder="请选择部门" filterable clearable
              >
                <el-option
                  v-for="item  in  deptList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="名称：">
              <el-input v-model="searchForm.userName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4" style="text-align:center;">
            <el-button size="mini" type="primary" @click="searchBtn">查询</el-button>
            <el-button size="mini" @click="emptyClick">清空</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-col :span="8" style="text-align:left;margin-bottom: 10px;">
        <el-button v-hasPermi="['/permission/userimport']" size="mini" type="success" @click="exportOr">导入
        </el-button>
      </el-col>
    </div>
    <el-table
      :data="tableData"
      border
      v-loading="loading"
      @selection-change="handleSelectionChange"
      @sort-change="getOrder"
      :row-key="bindRowKeys"
      style="width: 100%;">
      <el-table-column
        prop="userCode"
        header-align="center"
        align="center"
        label="人员编号">
      </el-table-column>

      <el-table-column
        prop="depName"
        header-align="center"
        align="center"
        label="部门名称">
      </el-table-column>

      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="gender"
        header-align="center"
        align="center"
        label="性别">
      </el-table-column>
      <el-table-column
        prop="birthday"
        header-align="center"
        align="center"
        label="出生日期">
      </el-table-column>

      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="导入时间">
        <template slot-scope="scope">
          {{ new Date(scope.row.createTime).toLocaleDateString().replaceAll('/', '-') }}
        </template>
      </el-table-column>
    </el-table>
    <div class="progress_lok_page">
      <page
        :hidden="tableData && tableData.length === 0"
        :total="total"
        :page.sync="pageNo"
        :limit.sync="pageSize"
        @pagination="handlePageChange"
      />
    </div>

    <el-dialog title="导入" :close-on-click-modal="false" :visible.sync="importWinVisible">
      <el-form label-width="100px" v-loading="importLoading">
        <el-row :gutter="10">
          <el-col :xs="24">
            <el-upload class="upload-demo" ref="upload"
                       :multiple="false" :file-list="fileList" :auto-upload="false"
                       :on-change="fileChange"
                       action="/hr-xc-human-resources-perform-manage/generator/roster/importRoster"
                       :on-success="handleUploadSuccess">
              <el-button type="danger" @click="downTemplate" style="margin-left: 10px;">下载模版</el-button>
              <el-button slot="trigger" type="primary">选取文件</el-button>
              <el-button style="margin-left: 10px;" type="success" @click="submitUpload">上传并导入</el-button>
              <div slot="tip" class="el-upload__tip">只能上传xlsx/xls文件</div>
            </el-upload>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        deptList: [],
        importWinVisible: false,
        importLoading: false,
        fileList: [],
        tableData: [],
        loadingPage: true,
        loading: true,
        currentPage: 0,
        pageNo: 1,
        pageSize: 10,
        total: 0,
        searchForm: {
          userName: '',// 名称
          deptId: ''
        }
      }
    },
    created() {
      this.getList ()
      this.getDeptList ()
    },
    methods: {
      // 获取列表
      getList() {
        var uu = {
          page: this.pageNo,
          pageSize: this.pageSize,
          userName: this.searchForm.userName,//申诉人
          deptId: this.searchForm.deptId//部门ID
        }
        var jsonString = JSON.stringify (uu)
        var yy = Encrypt (jsonString)
        var encodedString = encodeURIComponent (yy)
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/roster/list',
          data: encodedString
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var da = AesDecrypt (data.result)
            this.tableData = da.result.content
            this.total = da.result.total
            this.loading = false
            this.loadingPage = false
          } else {
            this.loading = false
            this.loadingPage = false
            this.$message.error ('获得花名册列表失败！')
            this.tableData = []
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 查询
      searchBtn() {
        this.loading = true
        this.page = 1
        this.getList ()
      },
      // 清空
      emptyClick() {
        this.searchForm.userName = ''
        this.searchForm.deptId = ''
      },
      /** 点击分页触发的事件 */
      handlePageChange(pageInfo) {
        this.loading = true
        this.pageNo = pageInfo.page
        this.pageSize = pageInfo.limit
        this.getList ()
      },
      // 多选
      handleSelectionChange(value) {
        this.checkedArr = value
        // element表格多选第一页选中往后翻一页全选，再回到第一页会再次选中第一页的数据，所以需要手动去重
        let hash = {}
        this.checkedArr = this.checkedArr.reduce ((preVal, curVal) => {
          hash[curVal.id] ? '' : hash[curVal.id] = true && preVal.push (curVal)
          return preVal
        }, [])
        // console.log(this.checkedArr,'this.checkedArr')
      },
      getOrder(param) {
        this.loading = true
        if (param.order === 'ascending') {
          this.searchForm.sort = 'asc'
          this.searchForm.sortField = param.prop
        } else if (param.order === 'descending') {
          this.searchForm.sort = 'desc'
          this.searchForm.sortField = param.prop
        } else {
          this.searchForm.sort = ''
          this.searchForm.sortField = ''
        }
        this.getList ()
      },
      bindRowKeys(row) {
        return row.id
      },
// 导入
      exportOr() {
        this.importWinVisible = true
        this.isUpload = false
      },

      // 判断是否选择文件
      fileChange(file, fileList) {
        if (file) {
          this.isUpload = true
        }
      },
      handleUploadSuccess(response, file, fileList) {
        this.importLoading = false
        this.$refs.upload.clearFiles ()
        if (response.code === '00000') {
          this.importWinVisible = false
          this.$message.info ('导入成功')
          this.getList ()
        } else if (response.code === '00900') {
          this.$message ({
            message: response.message,
            type: 'warning',
            duration: 2500,
          })
        } else {
          this.$message.warning (response.message)
        }
      },

      // 模版下载
      downTemplate() {
        axios ({
          url: '/hr-xc-human-resources-perform-manage/generator/roster/downloadTemp',
          method: 'post',
          responseType: 'blob'
        }).then (({ data }) => {
          if (data.size) {
            const downloadElement = document.createElement ('a')
            const href = window.URL.createObjectURL (data) // 创建下载的链接
            downloadElement.href = href
            downloadElement.download = decodeURI ('花名册模版.xlsx') // 下载后文件名
            document.body.appendChild (downloadElement)
            downloadElement.click () // 点击下载
            document.body.removeChild (downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL (href) // 释放掉blob对象
            this.loadingPage = false
            // this.$my_message.success ('导出成功')
            this.$message.success ('导出成功')
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      submitUpload() {
        this.$refs.upload.submit ()
      },
      getDeptList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/torgdept/queryAllDeptIdAndName'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var da = AesDecrypt (data.result)
            this.deptList = da
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      }
    }
  }
</script>

<style scoped>

</style>
